/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.registration.business.security.boundary;

import it.polimi.registration.business.security.entity.Event;
import it.polimi.registration.business.security.entity.Invite;
import it.polimi.registration.business.security.entity.InvitePK;
import it.polimi.registration.business.security.entity.InviteStatus;
import it.polimi.registration.business.security.entity.User;
import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.json.JSONException;

/**
 *
 * @author Stefano
 */


@Stateless
public class EventManager {

    //@Inject
//    private Event<Event> CreateNewEvent;
    
    @PersistenceContext
    EntityManager em;

    @Inject
    Principal principal;
    
    @EJB
    private SystemNotificationManager SNM;
    
    @EJB
    private InviteManager im;
    
    @EJB
    private WeatherService ws;
    
    private Event e;
    int idEvent = 1;

    public void save(Event event) {
        e=event;
        event.setCreator(getLoggedUser());
        em.persist(event);
       // CreateNewEvent.fire(event);
    }

    public void delete(Event event) {
        Event e = em.find(Event.class, event.getId());
        for (it.polimi.registration.business.security.entity.Invite invite : e.getInviteCollection()){
                em.remove(invite);
                em.flush();

            }
        
        for (it.polimi.registration.business.security.entity.SystemNotification sn : e.getSystemNotificationCollection()){
            em.remove(sn);
            em.flush();
        }
        em.refresh(e);
        em.remove(e);
    }

    public void update(Event event) {
        em.merge(event);
    }

    public User getLoggedUser() {
        return em.find(User.class, principal.getName());
    }

    public Event getEvent(String idEvent) {
        return em.find(Event.class, idEvent);
    }

    public Event merge(Event event) {
        return em.merge(event);
    }

    public void meteo() 
    {
       SNM.WeatherNotification(e);
                    
    }
    
    public void modifyDate(Event e, Calendar c, int value)
    {
        Event event = em.find(Event.class,e.getId());
        event.getStartTime().set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
        event.getEndTime().set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
        List<User> participants = new ArrayList();
        participants = im.findInvited(event);
                    if (participants != null)
                    {
                        if (!participants.isEmpty())
                        {
                            for (User user : participants) 
                            {
                                List<Invite> invites = im.findInvites(user, event);
                                for (Invite invite : invites) 
                                {
                                    im.delete(invite);
                                }
                                InvitePK invitePK = new InvitePK(user.getEmail(), event.getId());
                                Invite i = new Invite(invitePK);
                                i.setEvent(event);
                                i.setUser(user);
                                im.save(i);
                            }
                        }
                    }
        event.setWeatherCondition(value);
        em.merge(event);
    }
  
    public void updateWeather(Event value)
    {
        Event event=em.find(Event.class,value.getId());
        try {
            event.setWeatherCondition(ws.weatherService(event.getLocation(),event.getStartTime().getTime()));
        } catch (IOException ex) {
            Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JSONException ex) {
            Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            ws.checkWeatherEvent(event);
        } catch (IOException ex) {
            Logger.getLogger(EventManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        em.merge(event);
    }
    
}
